package com.fnn.mapper;
/**
 * 操作book表
 */

import com.fnn.entity.Book;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface BookMapper {

    //查询所有上架的书
    @Select("select * from book where book_status != 3")
    //把book实体与book数据表中的字段映射起来
    @Results(id="bookEntity", value = {
            @Result(column = "book_id",property = "book_id"),
            @Result(column = "book_name",property = "book_name"),
            @Result(column = "book_isbn",property = "book_isbn"),
            @Result(column = "book_press",property = "book_press"),
            @Result(column = "book_author",property = "book_author"),
            @Result(column = "book_pagination",property = "book_pagination"),
            @Result(column = "book_price",property = "book_price"),
            @Result(column = "book_upload_time",property = "book_upload_time"),
            @Result(column = "book_status",property = "book_status"),
            @Result(column = "borrower_id",property = "borrower_id"),
            @Result(column = "borrowed_time",property = "borrowed_time"),
            @Result(column = "return_book_time",property = "return_book_time"),
    })
    List<Book> selectAllByStatus();




    //查询新上架的书
    @Select("select * from book ORDER BY book_upload_time ASC LIMIT 9")
    @ResultMap(value = {"bookEntity"})
    List<Book> selectAllByTime();



    //通过book_id借阅书集,归还书籍
    @Update("update book set book_status=#{book_status},borrower_id=#{borrower_id},borrowed_time=#{borrowed_time},return_book_time=#{return_book_time} ")
    @ResultMap(value = {"bookEntity"})
    void updateBookBorrowAndReturn();



    //通过id查询
    @Select("select * from book where book_id=#{book_id}")
    @ResultMap(value = {"bookEntity"})
    Book selectById(int book_id);





    //通过书名或作者查询
    @Select("select * from book where book_name like concat('%',#{searchTerm},'%') or book_author like concat('%',#{searchTerm},'%')")
    @ResultMap(value = {"bookEntity"})
    List<Book> selectByNameOrAuthor(@Param("searchTerm") String searchTerm);




    //插入数据
    @Insert("insert into book(book_name, book_isbn, book_press, book_author, book_pagination, book_price, book_upload_time) values(#{book_name},#{book_isbn},#{book_press},#{book_author},#{book_pagination},#{book_price},#{book_upload_time})")
    @ResultMap(value = {"bookEntity"})
    int insertBook(Book book);





    //通过id更新数据
    @Update("update book set book_name=#{book_name},book_isbn=#{book_isbn},book_press=#{book_press},book_author=#{book_author},book_pagination=#{book_pagination},book_price=#{book_price},book_status=#{book_status} where book_id=#{book_id}")
    @ResultMap(value = {"bookEntity"})
    void updateBookById(Book book);



    //查询书籍，从指定位置开始查询10条
    @Select("SELECT * FROM book order by book_id asc LIMIT 10 OFFSET #{page}")
    @ResultMap(value = {"bookEntity"})
    List<Book> selectByPage(@Param("page") int page);




}
